package com.dai.wechat.util;

import com.dai.wechat.pojo.BaseMessage;
import com.dai.wechat.pojo.ReplyVO;
import com.thoughtworks.xstream.XStream;

import java.util.Map;

/**
 * 解析微信公众号服务器返回的xml文件
 * 转换成xml文件
 *  需要打导入dom4j的jar包
 * */
public class ReplyModel  {

    private final static String openId = "oiX7p50YftQIL1UqP4fqNLuuRInE";
    private static String templateId = "C69En65f6aCkUcVw5elsVzTjFkEJtiy2tk-eLUhxYuo";
    private static String CONSTELLATION_TEMPLATEID = "evhhMKsrczceL1vE7m01nOt4esyEbYS-HYiZF0BXmuY";

    public static String  responseReply( Map<String, String> parseXml){
        BaseMessage msg=null;
        //获取到消息的类型，如果还有其他类型的消息直接加case就可以
        String type = parseXml.get("MsgType");
        switch (type){
            case "text":
                // 调用处理文本消息的方法
                msg=dealTextMessage(parseXml);
                break;
            case "event":
                // 公众号菜单点击事件
                msg=dealEventMessage(parseXml);
                break;
        }

        //如果该消息不为null
        if(msg!=null){
            //调用把消息对象处理为xml数据包的方法
            return beanToxml(msg);
        }
        return null;
    }

    private static BaseMessage dealEventMessage(Map<String, String> parseXml) {
        BaseMessage msg=null;
          if("CLICK".equals(parseXml.get("Event"))){
              // 判断 key
              String eventKey = parseXml.get("EventKey");

              switch (eventKey){
                  case "V1001_TODAY_MUSIC":
                      // 点击了每日星座运势
                      msg = getConstellation(parseXml);
                      break;
              }

          }

        //如果该消息不为null
        if(msg!=null){
            //调用把消息对象处理为xml数据包的方法
            return msg;
        }
        return null;
    }

    private static BaseMessage getConstellation(Map<String, String> parseXml) {


        // 异步获取星座运势
        new Thread(()->{
            String fromUserName = parseXml.get("FromUserName");
            // 目标是触发事件的对象
            Pusher.push(fromUserName,CONSTELLATION_TEMPLATEID);
        }).start();

        return null;


    }

    ///把消息对象处理为xml数据包的方法
    private static String beanToxml(BaseMessage msg){
        XStream xStream =new  XStream();
        xStream.processAnnotations(ReplyVO.class);
        String xml = xStream.toXML(msg);
        return xml;
    }
 
    //处理文本消息
    private static BaseMessage dealTextMessage(Map<String, String> parseXml) {
        //将键值对的map直接转为对象，并为Content参数赋值
        String content = parseXml.get("Content");

        if(content != null && content.length() != 0){
            String message = null;
            if(content.contains("水")) message = "可爱的宝宝要多喝水水呀";
            else if(content.contains("爱")) message = "你是我最爱的宝贝，我要爱你一万年！";
            else if(content.contains("想")) message = "我也好想你，想要和你抱抱、贴贴、举高高！";
            else message = "欢迎来到滕敏专属公众号，这里是戴涛和滕敏爱的小窝~~";

            ReplyVO TM =new ReplyVO(parseXml,message);
            return TM;
        }

        ReplyVO TM =new ReplyVO(parseXml,"欢迎来到滕敏专属公众号，这里是戴涛和滕敏爱的小窝~~");
        return TM;
 
    }
}